package com.degon.hsyl.choosefile.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.degon.hsyl.choosefile.R;
import com.degon.hsyl.choosefile.bean.FileBean;

import java.util.List;

public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.ViewHolder> {

    private List<FileBean> fileList;
    private OnItemClickListener onItemClickListener;

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView fileIcon;
        TextView fileName;
        TextView fileSize;
        CheckBox chooseFile;
        RelativeLayout mainView;

        public ViewHolder(View itemView) {
            super(itemView);
            fileIcon = itemView.findViewById(R.id.file_icon);
            fileName = itemView.findViewById(R.id.file_name);
            fileSize = itemView.findViewById(R.id.file_size);
            chooseFile = itemView.findViewById(R.id.choose_file);
            mainView = itemView.findViewById(R.id.file_list_main_view);
        }
    }

    public FileListAdapter(List<FileBean> fileList) {
        this.fileList = fileList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.file_list_root_view, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        FileBean bean = fileList.get(position);
        //设置图标
        switch (bean.getType()) {
            case "doc":
            case "docx":
                holder.fileIcon.setImageResource(R.mipmap.word_icon);
                break;
            case "xls":
            case "xlsx":
                holder.fileIcon.setImageResource(R.mipmap.excel_icon);
                break;
            case "pdf":
                holder.fileIcon.setImageResource(R.mipmap.pdf_icon);
                break;
            case "ppt":
            case "pptx":
                holder.fileIcon.setImageResource(R.mipmap.ppt_icon);
                break;
            default:
                holder.fileIcon.setImageResource(R.drawable.default_image);
                break;
        }
        holder.fileName.setText(bean.getName());
        holder.fileSize.setText(bean.getSize());

        //设置点击事件
        if (onItemClickListener != null) {
            holder.mainView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onClick(view, position);
                }
            });

            holder.mainView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    onItemClickListener.onLongClick(view, position);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return fileList.size();
    }

    public interface OnItemClickListener {
        void onClick(View view, int position);

        void onLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }
}
